import { Main } from "../Main";
import { EncryptResUtil } from "./EncryptResUtil";
import { ShellUtil } from "./ShellUtil";
import { ManifestGenUtil } from "./ManifestGenUtil";
import { SvnUtil } from "./SvnUtil";
import { ConfigUtil } from "./ConfigUtil";
import { ChangeLuaCodeUtil } from "./ChangeLuaCodeUtil";
import { ProcessResSrcUtil } from "./ProcessResSrcUtil";

/**
 * ios打包工具，但是本工具只能进行打包前的预执行的动作，并不能真正的打出ipa来，ipa仍然需要用xcode来打
 * @author 赵占涛
 */
export class PublishIpaUtil {
    static exec(plat: string, cb: () => void) {

        // 生成manifest，
        let path = Main.codePath + "Resources/";
        let debugGateUrl = ConfigUtil.PlatCfg[plat].updateGateUrl + "debug/ios/";
        let debugCdnUrl = ConfigUtil.PlatCfg[plat].updateCdnUrl + "debug/ios/";
        let releaseGateUrl = ConfigUtil.PlatCfg[plat].updateGateUrl + "release/ios/";
        let releaseCdnUrl = ConfigUtil.PlatCfg[plat].updateCdnUrl + "release/ios/";
        let debugProjectManifest = path + "projectDebug.manifest";
        let releaseProjectManifest = path + "projectRelease.manifest";
        SvnUtil.getVersion(Main.codePath + "", (version: string) => {
            console.log("版本号是" + version);

            ProcessResSrcUtil.backupResourcesOrUseResources2(()=>{
                ProcessResSrcUtil.exec(plat, "ios", () => {
                    ManifestGenUtil.gen(path, debugGateUrl, debugCdnUrl, version, debugProjectManifest, "", (fileMd5) => {
                        ManifestGenUtil.gen(path, releaseGateUrl, releaseCdnUrl, version, releaseProjectManifest, "", (fileMd5) => {
                            console.log("Manifest生成 ok");
                            ShellUtil.exec(`
mv -f ${path}projectDebug.manifest ${path}res/projectDebug.manifest || exit 1
mv -f ${path}projectRelease.manifest ${path}res/projectRelease.manifest || exit 1
                                        `, cb);
                        });
                    });
                });
            });
        });
    }
}